package com.wva.user.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.wva.common.annotation.authority.Open;
import com.wva.user.domain.po.User;
import com.wva.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@Api(tags = "供微服务内部调用的接口")
@Slf4j
@RequestMapping("/user/rpc")
public class RpcController {
    @Resource
    UserService userService;

    @ApiOperation("根据ids获取用户昵称")
    @Open
    @GetMapping("/get-nicknames-by-ids")
    Map<Long, String> getNicknamesByIds(@RequestParam List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) return Collections.emptyMap();

        //查询用户集合
        List<User> users = userService.listByIds(ids);

        //集合转map
        return users.stream().collect(Collectors.toMap(
                User::getId,
                User::getNickname
        ));
    }
}
